package com.studentscore;

import com.studentscore.constants.Sqls;
import com.studentscore.tools.DataUtils;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.EtchedBorder;
import java.awt.Color;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * 主窗口
 * @author liyansheng
 * @date 2023/12/27
 */
public class IndexWin extends JFrame implements Sqls {



	private int isSelected = 0;

	Object[][] data;

	private JPanel contentPane;
	private JTextField jt_name;
	private JTextField jt_subject;
	private JTextField jt_id;
	private JTextField jt_score;
	private JTextField jt_teacher;
	private JTable table;
	private JTextField jt_date;



	void init() {
		try {
			ResultSet set = DataUtils.query(sql_all, new Object[]{});
			data = DataUtils.getSetArrays(set);
			table.setModel(new DefaultTableModel(data, columnNames));
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Create the frame.
	 */
	public IndexWin() {
		try {
			UIManager.setLookAndFeel(   UIManager.getSystemLookAndFeelClassName()); 
		} catch (Exception e) {
			// TODO: handle exception
		}
		setTitle("\u5b66\u751f\u6210\u7ee9\u7ba1\u7406");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 546, 494);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "\u6210\u7ee9\u5217\u8868", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_1.setBounds(10, 10, 507, 446);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 23, 487, 293);
		panel_1.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		init();

		table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (!e.getValueIsAdjusting()) { // 确保事件不会被重复触发
					int row = table.getSelectedRow();
					if (row != -1) {
						isSelected = (int) table.getValueAt(row, 1);
						// 获取选中行的数据
						jt_id.setText(table.getValueAt(row, 1).toString());
						jt_name.setText((String) table.getValueAt(row, 2));
						jt_teacher.setText((String) table.getValueAt(row, 5));
						BigDecimal value = (BigDecimal) table.getValueAt(row, 4);
						String stringValue = value.toString();
						jt_score.setText(stringValue);
						jt_subject.setText((String) table.getValueAt(row, 3));
						Date date = (Date) table.getValueAt(row, 6);
						SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
						String dateString = formatter.format(date);
						jt_date.setText(dateString);
					}
				}
			}
		});
		
		JLabel lblNewLabel = new JLabel("\u59d3\u540d\uff1a");
		lblNewLabel.setBounds(36, 326, 60, 15);
		panel_1.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u5b66\u751f\u0049\u0044\uff1a");
		lblNewLabel_1.setBounds(190, 326, 54, 15);
		panel_1.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("\u8001\u5e08\uff1a");
		lblNewLabel_2.setBounds(358, 330, 66, 15);
		panel_1.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("\u5b66\u79d1\uff1a");
		lblNewLabel_3.setBounds(36, 352, 66, 15);
		panel_1.add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("\u6210\u7ee9\uff1a");
		lblNewLabel_4.setBounds(190, 352, 66, 15);
		panel_1.add(lblNewLabel_4);
		
		jt_name = new JTextField();
		jt_name.setBounds(75, 324, 66, 21);
		panel_1.add(jt_name);
		jt_name.setColumns(10);
		
		jt_subject = new JTextField();
		jt_subject.setBounds(75, 349, 66, 21);
		panel_1.add(jt_subject);
		jt_subject.setColumns(10);
		
		jt_id = new JTextField();
		jt_id.setToolTipText("\u65B0\u589E\u65F6\u53EF\u4E0D\u586B");
		jt_id.setBounds(228, 324, 66, 21);
		panel_1.add(jt_id);
		jt_id.setColumns(10);
		
		jt_score = new JTextField();
		jt_score.setBounds(228, 351, 66, 21);
		panel_1.add(jt_score);
		jt_score.setColumns(10);
		
		jt_teacher = new JTextField();
		jt_teacher.setBounds(400, 326, 66, 21);
		panel_1.add(jt_teacher);
		jt_teacher.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("\u8003\u8bd5\u65e5\u671f\uff1a");
		lblNewLabel_5.setBounds(359, 357, 54, 15);
		panel_1.add(lblNewLabel_5);
		
		jt_date = new JTextField();
		jt_date.setBounds(400, 352, 66, 21);
		panel_1.add(jt_date);
		jt_date.setColumns(10);
		
		JButton jb_add = new JButton("\u6DFB\u52A0\u6570\u636E");
		jb_add.setBounds(23, 398, 88, 23);
		panel_1.add(jb_add);
		
		JButton jb_update = new JButton("\u4FEE\u6539");
		jb_update.setBounds(121, 398, 75, 23);
		panel_1.add(jb_update);
		
		JButton jb_delete = new JButton("\u5220\u9664\u9009\u4E2D");
		jb_delete.setBounds(206, 398, 88, 23);
		panel_1.add(jb_delete);
		
		JButton jb_reset = new JButton("\u7F6E\u7A7A");
		jb_reset.setBounds(370, 398, 60, 23);
		panel_1.add(jb_reset);

		JButton jb_out = new JButton("\u9000\u51fa");
		jb_out.setBounds(435, 398, 60, 23);
		panel_1.add(jb_out);
		
		JButton jb_find = new JButton("\u68C0\u7D22");
		jb_find.setBounds(304, 398, 60, 23);
		panel_1.add(jb_find);

		jb_out.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				new LoginWindow();
			}
		});

		jb_find.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nameText = jt_name.getText();
				String teacherText = jt_teacher.getText();
				String subjectText = jt_subject.getText();
				String scoreText = jt_score.getText();
				String dateText = jt_date.getText();
				String sql5_search_plus = "SELECT * FROM student_grades WHERE 1 = 1 ";
				ArrayList<Object> list = new ArrayList<>();
				if (!jt_id.getText().equals("")) {
					sql5_search_plus += "and id= ? ";
					list.add(Integer.parseInt(jt_id.getText()));
				}
				if (!nameText.equals("")) {
					sql5_search_plus += "and name= ? ";
					list.add(nameText);
				}
				if (!teacherText.equals("")) {
					sql5_search_plus += "and teacher= ? ";
					list.add(teacherText);
				}
				if (!subjectText.equals("")) {
					sql5_search_plus += "and subject= ? ";
					list.add(subjectText);
				}
				if (!scoreText.equals("")) {
					sql5_search_plus += "and score= ? ";
					list.add(scoreText);
				}
				if (!dateText.equals("")) {
					sql5_search_plus += "and dateText= ? ";
					list.add(dateText);
				}

				try {
					System.out.println(sql5_search_plus);
					ResultSet set = DataUtils.query(sql5_search_plus, list.toArray(new Object[list.size()]));
					table.setModel(new DefaultTableModel(DataUtils.getSetArrays(set), columnNames));
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		jb_reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearup();
			}
		});
		jb_delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					DataUtils.Update(sql3_delete, new Object[]{
							isSelected
					});
					JOptionPane.showMessageDialog(null,"\u006f\u006b\uff01");
					init();
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		jb_update.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nameText = jt_name.getText();
				String teacherText = jt_teacher.getText();
				String subjectText = jt_subject.getText();
				String scoreText = jt_score.getText();
				String dateText = jt_date.getText();
				try {
					DataUtils.Update(sql4_update, new Object[]{
							nameText, subjectText, scoreText, teacherText, dateText, isSelected
					});
					init();
					clearup();
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		jb_add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nameText = jt_name.getText();
				String teacherText = jt_teacher.getText();
				String subjectText = jt_subject.getText();
				String scoreText = jt_score.getText();
				String dateText = jt_date.getText();
				if (nameText.equals("") || teacherText.equals("") || subjectText.equals("") || scoreText.equals("") || dateText.equals("")) {
					JOptionPane.showMessageDialog(null, "\u0063\u0061\u006e\u006e\u006f\u0074\u0020\u0065\u006d\u0070\u0074\u0079\uff01");
				} else {
					try {
						DataUtils.Update(sql2_add, new Object[]{
								nameText, subjectText, scoreText, teacherText,dateText
						});
						clearup();
						init();
					} catch (SQLException ex) {
						throw new RuntimeException(ex);
					}
				}

			}
		});
	}

	private void clearup() {
		jt_id.setText("");
		jt_name.setText("");
		jt_teacher.setText("");
		jt_subject.setText("");
		jt_score.setText("");
		jt_date.setText("");
		init();
	}

}
